'use strict';
(function(){
  var baseImageUrl = 'http://static.winnovator.cn/scale/0011/';

  var LINGDAOZHE = 'lingdaozhe';
  var ZHINANG = 'zhinang';
  var GOUTONGZGE = 'goutongzhe';
  var ZHIXINGZHE = 'zhixingzhe';
  var dimData =
  [
    {
      id : 'lingdaozhe',
      name:'领导者',
      desc:'这类孩子性格一般比较自信、稳重，有出色的影响力和号召力，能够调动身边的人，共同实现目标，在关键的时候也能挺身而出，有优秀的责任感和担当；在团队中一般是制定目标，统筹成员行动的作用。',
      url: baseImageUrl + 'lingdaozhe.png'
    },
    {
      id : 'zhinang',
      name:'智囊',
      desc:'这类孩子给人的感觉会比较活泼、头脑开明，对新事物的接受和学习能力较强，也有比较出色的洞察力和分析判断能力。因此他们在团队中一般担任的是“军师”的角色，经常帮团队和其他人出谋划策、解决困难。',
       url: baseImageUrl + 'zhinang.png'
    },
    {
      id : 'goutongzhe',
      name:'沟通者',
      desc:'这类孩子是团队的粘合剂，具有出色的情商和社交能力，一般比较温和又不失敏感，；在团队中起到凝聚作用，促进成员之间的合作，同时也是团队与外界沟通的渠道；他们似乎是天生的乐天派，总能看到事物的积极方面，因此也能较快地适应新环境。',
       url: baseImageUrl + 'goutongzhe.png'
    },
    {
       id : 'zhixingzhe',
       name:'执行者',
       desc:'相对于其他孩子，这类孩子显得比较“低调”而游离，但他们也是团队计划得已实现的核心，他们比较勤奋、务实，能够独立思考和行动，他们有一种坚持和韧劲，在面对困难时能迸发出强大的力量。',
        url: baseImageUrl + 'zhixingzhe.png'
     },
  ];

  var sceneData = [
   {
        id:'1',
        name:'三国演义',
        foreground: baseImageUrl + 'sanguo-foreground.jpg',
        background: baseImageUrl + 'sanguo-background.jpg',
        roles: [
          {
            id:'liubei',
            name:'刘备',
            url: baseImageUrl + "liubei.jpg",
            dim:LINGDAOZHE
          },
          {
            id:'guanyu',
            name:'关羽',
            url:baseImageUrl + "guanyu.jpg",
            dim:GOUTONGZGE
          },

          {
            id:'zhaoyun',
            name:'赵云',
            url:baseImageUrl + 'zhaoyun.jpg',
            dim:ZHIXINGZHE
          },
          {
            id:'zhuge',
            name:'诸葛',
            url:baseImageUrl + 'zhuge.jpg',
            dim:ZHINANG
          }
        ]
      },
  {
        id:'2',
        name:'西游记',
        foreground: baseImageUrl + 'xiyou-foreground.jpg',
        background: baseImageUrl + 'xiyou-background.jpg',
        roles: [
          {
            id:'tangseng',
            name:'唐僧',
            url:baseImageUrl + 'tangseng.jpg',
            dim:LINGDAOZHE
          },
          {
            id:'wukong',
            name:'孙悟空',
            url:baseImageUrl + 'wukong.jpg',
            dim:ZHINANG
          },
          {
            id:'bajie',
            name:'猪八戒',
            url:baseImageUrl + "bajie.jpg",
            dim:GOUTONGZGE
          },
          {
            id:'shaseng',
            name:'沙僧',
            url: baseImageUrl + "shaseng.jpg",
            dim:ZHIXINGZHE
          }
        ]
      },
  {
    id:'3',
    name:'水浒传',
    foreground: baseImageUrl + 'shuihu-foreground.jpg',
    background: baseImageUrl + 'shuihu-background.jpg',
    roles: [
     {
        id:'songjiang',
        name:'宋江',
        url:baseImageUrl + 'songjiang.jpg',
        dim:LINGDAOZHE
      },
     {
       id:'wusong',
       name:'武松',
       url:baseImageUrl + 'wusong.jpg',
       dim:ZHIXINGZHE
     },
      {
        id:'likui',
        name:'李逵',
        url:baseImageUrl + "likui.jpg",
        dim:GOUTONGZGE
      },

      {
        id:'wuyong',
        name:'吴用',
        url:baseImageUrl + 'wuyong.jpg',
        dim:ZHINANG
      },
      {
        id:'linchong',
        name:'林冲',
        url: baseImageUrl + "linchong.jpg",
        dim:ZHIXINGZHE
      }
    ]
  },
  {
         id:'4',
         name:'天龙八部',
         foreground: baseImageUrl + 'tianlongbabu-foreground.jpg',
         background: baseImageUrl + 'tianlongbabu-background.jpg',
         roles: [
           {
             id:'qiaofeng',
             name:'乔峰',
             url:baseImageUrl + 'qiaofeng.jpg',
             dim:LINGDAOZHE
           },

            {
              id:'xuzhu',
              name:'虚竹',
              url:baseImageUrl + 'xuzhu.jpg',
              dim:ZHIXINGZHE
            },
            {
               id:'duanyu',
               name:'段誉',
               url: baseImageUrl + "duanyu.jpg",
               dim:GOUTONGZGE
             },
           {
             id:'azi',
             name:'阿紫',
             url:baseImageUrl + "azi.jpg",
             dim:ZHINANG
           }
         ]
       },
  {
        id:'5',
        name:'复仇者联盟',
        foreground: baseImageUrl + 'fuchou-foreground.jpg',
        background: baseImageUrl + 'fuchou-background.jpg',
        roles: [
          {
            id:'gangtiexia',
            name:'钢铁侠',
            url:baseImageUrl + "gangtiexia.jpg",
            dim:ZHINANG
          },
          {
            id:'meidui',
            name:'美国队长',
            url:baseImageUrl + 'meidui.jpg',
            dim:LINGDAOZHE
          },
          {
            id:'haoke',
            name:'浩克',
            url: baseImageUrl + "haoke.jpg",
            dim:ZHIXINGZHE
          },
          {
            id:'suoer',
            name:'索尔',
            url:baseImageUrl + 'suoer.jpg',
            dim:GOUTONGZGE
          },
          {
            id:'heiguafu',
            name:'黑寡妇',
            url:baseImageUrl + 'heiguafu.jpg',
            dim:GOUTONGZGE
          },
          {
            id:'yingyan',
            name:'鹰眼',
            url:baseImageUrl + 'yingyan.jpg',
            dim:ZHIXINGZHE
          }
        ]
      },
  {
            id:'6',
            name:'银河护卫队',
            foreground: baseImageUrl + 'yinhehuweidui-foreground.jpg',
            background: baseImageUrl + 'yinhehuweidui-background.jpg',
            roles: [
              {
                id:'xingjue',
                name:'星爵',
                url:baseImageUrl + 'xingjue.jpg',
                dim:LINGDAOZHE
              },
              {
                id:'huanxiong',
                name:'火箭浣熊',
                url:baseImageUrl + "huanxiong.jpg",
                dim:ZHINANG
              },
              {
                id:'shuren',
                name:'树人格鲁特',
                url:baseImageUrl + 'shuren.jpg',
                dim:GOUTONGZGE
              },
              {
                id:'kamola',
                name:'卡魔拉',
                url:baseImageUrl + 'kamola.jpg',
                dim:ZHIXINGZHE
              },
              {
                id:'huimiezhe',
                name:'毁灭者德拉克斯',
                url: baseImageUrl + "huimiezhe.jpg",
                dim:ZHIXINGZHE
              }



            ]
          },
  {
        id:'7',
        name:'指环王',
        foreground: baseImageUrl + 'zhihuanwan-foreground.jpg',
        background: baseImageUrl + 'zhihuanwan-background.jpg',
        roles: [
          {
            id:'gandaofu',
            name:'甘道夫',
            url:baseImageUrl + 'gandaofu.jpg',
            dim:ZHINANG
          },
          {
            id:'alagong',
            name:'阿拉贡',
            url:baseImageUrl + "alagong.jpg",
            dim:LINGDAOZHE
          },
          {
            id:'foleiduo',
            name:'弗雷多',
            url: baseImageUrl + "foleiduo.jpg",
            dim:ZHIXINGZHE
          },
          {
            id:'shanmu',
            name:'山姆',
            url:baseImageUrl + 'shanmu.jpg',
            dim:GOUTONGZGE
          },

           {
             id:'laigelasi',
             name:'莱戈拉斯',
             url:baseImageUrl + 'laigelasi.jpg',
             dim:ZHINANG
           },
          {
            id:'jinpi',
            name:'金雳',
            url:baseImageUrl + 'jinpi.jpg',
            dim:ZHIXINGZHE
          }
        ]
      },
  {
        id:'8',
        name:'哈利波特',
        foreground: baseImageUrl + 'halibote-foreground.jpg',
        background: baseImageUrl + 'halibote-background.jpg',
        roles: [
          {
            id:'hali',
            name:'哈利',
            url:baseImageUrl + "hali.jpg",
            dim:LINGDAOZHE
          },
          {
            id:'luoen',
            name:'罗恩',
            url:baseImageUrl + 'luoen.jpg',
            dim:GOUTONGZGE
          },
          {
            id:'hemin',
            name:'赫敏',
            url: baseImageUrl + "hemin.jpg",
            dim:ZHINANG
          },

          {
            id:'nawei',
            name:'纳威',
            url:baseImageUrl + 'nawei.jpg',
            dim:ZHIXINGZHE
          }
        ]
      },
  {
        id:'9',
        name:'哆啦A梦',
        foreground: baseImageUrl + 'duola-foreground.jpg',
        background: baseImageUrl + 'duola-background.jpg',
        roles: [
          {
            id:'duolaameng',
            name:'哆啦A梦',
            url: baseImageUrl + "duolaameng.jpg",
            dim:ZHINANG
          },
          {
            id:'daxiong',
            name:'大雄',
            url:baseImageUrl + "daxiong.jpg",
            dim:'toutongzhe'
          },
          {
            id:'panghu',
            name:'胖虎',
            url:baseImageUrl + 'panghu.jpg',
            dim:LINGDAOZHE
          },

           {
             id:'xiaofu',
             name:'小夫',
             url:baseImageUrl + 'xiaofu.jpg',
             dim:ZHIXINGZHE
           },
          {
            id:'jingxiang',
            name:'静香',
            url:baseImageUrl + 'jingxiang.jpg',
            dim:ZHIXINGZHE
          }
        ]
      },

  {
    id:'10',
    name:'爸爸去哪儿',
    foreground: baseImageUrl + 'babaquna-foreground.jpg',
    background: baseImageUrl + 'babaquna-background.jpg',
    roles: [
    {
        id:'nuoyi',
        name:'诺一',
        url:baseImageUrl + 'nuoyi.jpg',
        dim:GOUTONGZGE
      },
      {
        id:'kangkang',
        name:'康康',
        url: baseImageUrl + "kangkang.jpg",
        dim:LINGDAOZHE
      },
      {
        id:'xiatian',
        name:'夏天',
        url:baseImageUrl + 'xiatian.jpg',
        dim:GOUTONGZGE
      },
      {
        id:'dajun',
        name:'大俊',
        url:baseImageUrl + "dajun.jpg",
        dim:ZHINANG
      },
      {
        id:'xuanxuan',
        name:'轩轩',
        url:baseImageUrl + 'xuanxuan.jpg',
        dim:ZHIXINGZHE
      }
    ]
  },










];
  window.roundData = sceneData;
  //这三个都是roundData的全部或者部分copy
  var roundOneCopy = null;
  var roundTwoCopy = [];
  var roundThreeCopy = [];
  var roundResult = [];

  var userSelectResult = {
    qList : [],
    uSelect: [],
    sortedSelect :[]
  }

  //记录回合阶段
  var nowPhase = 0;

  function selectStartMember(arr, shuffle, n ){
    var tempArr = [];

    if(shuffle){
      for(var i = 0; i < n; i ++){
        var r = _.random(0, arr.length-1);
        tempArr.push(arr.splice(r,1)[0]);
      }
    }else{
      tempArr = arr.splice(0,4);
    }

    return tempArr;
  }



  function generateRoundOneItem(){
    if(roundOneCopy === null){
      roundOneCopy = _.clone(roundData);
    }

    var temp = selectStartMember(roundOneCopy, true, 4);

    userSelectResult.qList.push(_.pluck(temp, 'id'));

    return _.map(temp, function(oneRoundData){
      return {
        results: oneRoundData.id,
        caption: oneRoundData.descOne
      }
    });
  }
  function generateRoundTwoItem(){
    var temp = selectStartMember(roundTwoCopy, true, 4);

    userSelectResult.qList.push(_.pluck(temp, 'id'));

    return _.map(temp, function(oneRoundData){
      return {
        results: oneRoundData.id,
        caption: oneRoundData.descTwo
      }
    });
  }

  var tempRoundThreeData = null;
  function generateRoundThreeItem(){
    if(tempRoundThreeData === null){
      tempRoundThreeData = [];
      var temp = _.shuffle(roundThreeCopy);
      _.each(temp, function(oneRoundData){
        tempRoundThreeData.push({
          results: oneRoundData.id,
          caption: oneRoundData.descThree[0]
        });
      });
      _.each(temp, function(oneRoundData){
        tempRoundThreeData.push({
          results: oneRoundData.id,
          caption: oneRoundData.descThree[1]
        });
      });
    }

    userSelectResult.qList.push(_.pluck(tempRoundThreeData, 'id'));
    return selectStartMember(tempRoundThreeData, false, 4);
  }

  window.roundDataUtil = {

    getAllSceneData : function(){
        return sceneData;
    },
    //通过qid,生成qid对应题目的题目
    generateData : function(qid){
      if(qid <= 6){
        nowPhase = 1;
        //获得第一个回合的数据
        return generateRoundOneItem();
      }else if(qid <= 9){
        nowPhase = 2;
        //获得第二个回合的数据
        return generateRoundTwoItem();
      }else{
        nowPhase = 3;
        //获得第三个回合的数据
        return generateRoundThreeItem();
      }
    },
    selectItem: function(qid, selectResult){
      var selectItem = _.find(roundData, function(oneData){
        return JSON.parse(oneData.id) === JSON.parse(selectResult);
      });
      if(nowPhase === 1){
        roundTwoCopy.push(selectItem);
      }else if(nowPhase === 2){
        roundThreeCopy.push(selectItem);
      }else{
        roundResult.push(selectItem);
      }
    },
    selectItemBatch:function(qid,selectResultArr){
      var that = this;
      _.each(selectResultArr, function(oneSelectResult){
        that.selectItem(qid, oneSelectResult);
      });

      userSelectResult.uSelect.push(selectResultArr);
    },
    getRouondResult: function(){
      return _.uniq(_.sortBy(roundResult, function(oneData){return JSON.parse(oneData.id)}));
    },
    getUserSelectResult:function(userSelected){
        //取出Key
      var partition = _.partition(_.rest(_.toArray(userSelected)), function(oneSelect , index){
          return index%2;
      });

      var partitionEven = partition[0];//偶数
      var partitionOdd = partition[1];// 奇数


      var wantTo = _.sortBy(_.groupBy(partitionOdd), function(one){
        return one.length;
      });
      var realTo = _.sortBy(_.groupBy(partitionEven), function(one){
       return one.length;
     });

     var wantToMax = _.max(wantTo, function(one){return one.length}).length;
     var realToMax = _.max(realTo, function(one){return one.length}).length;

     var wantToObj = _.map(_.filter(wantTo, function(oa){return oa.length === wantToMax}),
          _.first
        );
     var realToObj = _.map(_.filter(realTo, function(oa){return oa.length === realToMax}),
        _.first);

      var wantToResult = [];
      _.each(wantToObj, function(one){
          wantToResult.push(_.find(dimData, function(oneData){ return oneData.id === one}));
      });

      var realToResult = [];
      _.each(realToObj, function(one){
          realToResult.push(_.find(dimData, function(oneData){ return oneData.id === one}));
      });

      var uniq = _.uniq(_.union(wantToResult, realToResult));

      var userSelectResult = {
        'wantTo' : wantToResult,
        'realTo' : realToResult,
        'uniq': uniq
      };

      console.log(userSelectResult);

      return userSelectResult;
    }
  }


var questionCaption1 = '如果可以选择，你<span class="questionCaption">最希望</span>孩子像以下哪一个角色呢？';
var questionCaption2 = '<span class="questionCaption">实际上</span>，你观察你的孩子性格最像哪一个角色呢？';
window.yoparentData = {
  'id':11,
  'cate_id': '1',
  'title': '儿童团队性格测试',
  'imgSrc': 'http://static.winnovator.cn/scale/0011/face.jpg?t=' + Math.random(),
  'imgWidth': 750,'imgHeight': 500,
  'description': '你真正了解你孩子的性格吗？在人生的“棋盘”上，孩子的性格更适合哪种角色呢？以下这个测试将帮助你了解你对孩子的期望和孩子真正适合的角色。（适用年龄范围：3岁以上，5-12岁尤其合适）<br / ><div style="text-align: center"><img src="http://static.winnovator.cn/scale/0011/expert.png?t=2"  style="width: 100%;height: auto; margin-top: 25px;"/></div> ',
  'isCheckResult':false,
  'showCheckResult':false,
  'share': {
    'title': '你的孩子在团队中是什么角色？ | 优儿测试',
    'description':''
  },
  'questions': [
    {
      'id': '1',
      'caption': questionCaption1,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 500,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '2',
      'caption': questionCaption2,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 500,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
         {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '3',
      'caption': questionCaption1,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 500,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
         {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '4',
      'caption': questionCaption2,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 500,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
         {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '5',
      'caption': questionCaption1,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 500,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '6',
      'caption': questionCaption2,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 500,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '7',
      'caption': questionCaption1,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 500,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
         {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '8',
      'caption': questionCaption2,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 500,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
         {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '9',
      'caption': questionCaption1,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 500,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
         {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    },
    {
      'id': '10',
      'caption': questionCaption2,
      'img_src': '',
      'imgWidth': 750,'imgHeight': 500,
      'layout': { 'cols': '1','img': 'left'},
      'answers': [
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0},
        {'caption': 'A','results': 0}
      ]
    }
  ],
  'tpl': 'default'
};
})();
